﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;



namespace KinnectInput
{
    public class KinectFlatInteraction
    {
        
        public SkeletalManager skellManager { get; private set; }
        private Texture2D _handTex;
        public Point HandPosPoint { get; set; }
        public Texture2D HandTex {
            get
            {
                return _handTex;
            }
            set
            {
                _handTex = value;
                flatHand = value.Bounds;
                //r_hand2 = value.Bounds;
                //l_hand2 = value.Bounds;
            }
        }
         
        GraphicsDevice gDev;
        public Rectangle flatHand;
        //public Rectangle r_hand2;
        //public Rectangle l_hand2;
        public KinectFlatInteraction(SkeletalManager skellManager, GraphicsDevice gDev)
        {
            this.skellManager=skellManager ;
            this.HandTex = new Texture2D(gDev, 1, 1);
            HandTex.SetData(new Color[] { Color.White });
            this.gDev = gDev;
        }
        public void Update()
        {
            //Vector2 hand=skellManager.JointPosition[ Microsoft.Research.Kinect.Nui.JointID.HandRight ];
            //Vector2 hand = skellManager.JointPosition[Microsoft.Research.Kinect.Nui.JointID.HandRight];
            //hand.X -= HandTex.Width / 2;
            //hand.Y -= HandTex.Height / 2;
            //r_hand2.Location=new Point( (int)hand.X,(int)hand.Y);

            //hand = skellManager.JointPosition[Microsoft.Research.Kinect.Nui.JointID.HandLeft];
            //hand.X -= HandTex.Width / 2;
            //hand.Y -= HandTex.Height / 2;
            //l_hand2.Location = new Point((int)hand.X, (int)hand.Y);
            if (skellManager != null && skellManager.MainUser != null)
            {
                this.HandPosPoint = new Point(skellManager.MainUser.getMouseHand(gDev.Viewport.Width, gDev.Viewport.Height).X, skellManager.MainUser.getMouseHand(gDev.Viewport.Width, gDev.Viewport.Height).Y);
                
                flatHand.Location = HandPosPoint;
                flatHand.X = flatHand.X - ((int)(HandTex.Width / 2));
                flatHand.Y = flatHand.Y - ((int)(HandTex.Height / 2));
                
            }
            else
                flatHand.Location = Point.Zero;
        }
        public void Draw(SpriteBatch sb)
        {
            //sb.Draw(HandTex, l_hand2, new Color(0.6f,0.6f,1f));
            //sb.Draw(HandTex, r_hand2, null, new Color(1f, 0.6f, 0.6f), 0, Vector2.Zero, SpriteEffects.FlipHorizontally, 0);
            
            if (skellManager.JointPosition[Microsoft.Research.Kinect.Nui.JointID.HandRight].Y < skellManager.JointPosition[Microsoft.Research.Kinect.Nui.JointID.HipCenter].Y && skellManager.JointPosition[Microsoft.Research.Kinect.Nui.JointID.HandRight].Y < skellManager.JointPosition[Microsoft.Research.Kinect.Nui.JointID.HipCenter].Y)
            {
                sb.Draw(HandTex, flatHand, null, Color.White, 0, Vector2.Zero, SpriteEffects.FlipHorizontally, 0);
            }
            else
            {
                
                sb.Draw(HandTex, flatHand, Color.White);
            }
        }
    }
}
